package dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.handlers.BeanHandler;

import bean.Message;
import dao.MessageDao;


//Apply
public class MessageDaoImpl extends BaseDao implements MessageDao{

	@Override
	public Integer selectMessagebyid(Integer id) {
		try {
			Object query2=query.query("select * from message where messageid=?",id,new BeanHandler<Message>(Message.class));
			return Integer.parseInt(query2.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer deleteMessagebyid(Integer id) {
		try {
			return query.update("delete from message where messageid=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer updateMessage(Message info) {
		String sql="update message set messagesenderid=?,messagereceiverid=?,messagetype=?,"
				+ "messagecontent=?,messagetime=?, messageifread=? where messageid=?";
		try {
			return query.update(sql,info.getMessagesenderid(),info.getMessagereceiverid(),info.getMessagetype(),info.getMessagecontent(),
					info.getMessagetime(),info.getMessageifread());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer addMessage(Message info) {
		String sql="insert into message(messagesenderid,messagereceiverid,messagetype,messagecontent,messagetime,messageifread) VALUES(?,?,?,?,?,?)";
		try {
			return query.update(sql,info.getMessagesenderid(),info.getMessagereceiverid(),info.getMessagetype(),info.getMessagecontent(),
					info.getMessagetime(),info.getMessageifread());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

}
